﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocationUI : MonoBehaviour
{
    public TextAsset provinceFile;
    public TextAsset cityFile;

    public ScrollSelectUI provinceUI;
    public ScrollSelectUI cityUI;

    Province[] provinces;
    City[] allCities;
    City[] curCities;

    int m_ProvinceIndex;
    int m_CityIndex;

    protected string curCityValue;
    protected string curProviinceValue;

    protected Action<string,string> m_OnSelect;
    public void Init(Action<string,string> onSelect)
    {
        this.m_OnSelect = onSelect;

        provinces = Newtonsoft.Json.JsonConvert.DeserializeObject<Province[]>(this.provinceFile.text);
        allCities = Newtonsoft.Json.JsonConvert.DeserializeObject<City[]>(this.cityFile.text);

        string[] provNames = provinces.Map(e => e.name).AsArray();
        provinceUI.Init(provNames);

        OnSelectProvince(0);

        //provinceUI.onShowIndex = null;
        provinceUI.onIndex = OnSelectProvince;
        cityUI.onIndex = OnSelectCity;
    }

    void OnSelectProvince(int index)
    {
        m_ProvinceIndex = index;

        Province province = provinces[index];
        curCities = this.allCities.AllWhere(e => e.ProID == province.ProID).AsArray();

        var cityNames = curCities.Map(e=>e.name).AsArray();

        cityUI.Init(cityNames);

        SetValue(provinces[m_ProvinceIndex].name, curCities[0].name);
    }
    private void OnSelectCity(int index)
    {
        m_CityIndex = index;

        SetValue(provinces[m_ProvinceIndex].name, curCities[m_CityIndex].name);
    }


    void SetValue(string province, string city)
    {
        if (province.Equals(city))
        {
            curCityValue = city;
        }
        else
        {
            curProviinceValue = province;
            curCityValue = city;
        }
    }

    public void OnClickConfirm()
    {
        this.m_OnSelect?.Invoke(this.curProviinceValue,this.curCityValue);

        Close();
    }

    public void OnClickGoBack()
    {
        Close();
    }

    void Close()
    {
        gameObject.SetActive(false);
    }
}

//public class LocationUI : ScrollSelectUI
//{
//    public TextAsset provinceFile;
//    public TextAsset cityFile;

//    List<ScrollSelectItem> m_ProvinceItems = new List<ScrollSelectItem>();
//    List<ScrollSelectItem> m_CityItems = new List<ScrollSelectItem>();
//    public Transform provItemsParent;
//    public Transform cityItemsParent;
//    public ScrollSelectItem itemPref;

//    public ItemListManagerVertitcal provinceListManagerVertical;
//    public ItemListManagerVertitcal cityListManagerVertical;

//    int m_CurProvinceIndex;
//    int m_ProvinceShowingIndex;

//    int m_CurCityIndex;
//    int m_CityShowingIndex;

//    Province[] provinces;
//    City[] cities;
   

//    void OnEnable()
//    {

//    }

//    public override void Init(Action<int,string> onSelect)
//    {
//        base.Init(onSelect);

//        provinces = Newtonsoft.Json.JsonConvert.DeserializeObject<Province[]>(this.provinceFile.text);
//        cities = Newtonsoft.Json.JsonConvert.DeserializeObject<City[]>(this.cityFile.text);
//        //GameDebug.Log($"provinces:{provinces.Length}");
//        provinceListManagerVertical.maxIndex = provinces.Length - 1;

//        UnityFunctions.ClearList(m_ProvinceItems);
//        for(int i = 0; i < provinces.Length; i++)
//        {
//            var newItem = Instantiate(this.itemPref, provItemsParent);
//            newItem.gameObject.SetActive(true);

//            if (i == 0)
//            {
//                newItem.ShowActive(false);
//            }
//            else
//            {
//                newItem.ShowDisctive(false);
//            }

//            newItem.Init(provinces[i].name);

//            m_ProvinceItems.Add(newItem);
//        }

//        int defaultProvinceIndex = 0;
//        OnSelectProvince(defaultProvinceIndex);
//    }

//    void OnSelectProvince(int index)
//    {
//        Province province = provinces[index];
//        var pcities = this.cities.AllWhere(e => e.ProID == province.ProID).AsArray();
//        cityListManagerVertical.maxIndex = pcities.Length - 1;

//        GameDebug.Log($"province:{province.name}  pcities:{pcities.Length}");

//        UnityFunctions.ClearList(m_CityItems);
//        for (int i = 0; i < pcities.Length; i++)
//        {
//            var newItem = Instantiate(this.itemPref, cityItemsParent);
//            newItem.gameObject.SetActive(true);

//            if (i == 0)
//            {
//                SetValue(i,province.name,pcities[i].name);
//                newItem.ShowActive(false);
//            }
//            else
//            {
//                newItem.ShowDisctive(false);
//            }

//            newItem.Init(pcities[i].name);

//            m_CityItems.Add(newItem);
//        }
//        var pos = cityListManagerVertical.transform.localPosition;
//        pos.y = 0;
//        cityListManagerVertical.transform.localPosition = pos;
//    }


//    // Update is called once per frame
//    void Update()
//    {
//        if(m_ProvinceShowingIndex != this.provinceListManagerVertical.showingIndex)
//        {
//            m_ProvinceShowingIndex = this.provinceListManagerVertical.showingIndex;

//            for(int i = 0; i < this.m_ProvinceItems.Count; i++)
//            {
//                if(i == m_ProvinceShowingIndex)
//                {
//                    m_ProvinceItems[i].ShowActive();
//                }
//                else
//                {
//                    m_ProvinceItems[i].ShowDisctive();
//                }
//            }
//        }

//        if (m_CityShowingIndex != this.cityListManagerVertical.showingIndex)
//        {
//            m_CityShowingIndex = this.cityListManagerVertical.showingIndex;

//            for (int i = 0; i < this.m_CityItems.Count; i++)
//            {
//                if (i == m_CityShowingIndex)
//                {
//                    SetValue(m_CurProvinceIndex, this.provinces[m_CurProvinceIndex].name,m_CityItems[i].value);
//                    m_CityItems[i].ShowActive();
//                }
//                else
//                {
//                    m_CityItems[i].ShowDisctive();
//                }
//            }
//        }

//        if (m_CurProvinceIndex != provinceListManagerVertical.curIndex)
//        {
//            m_CurProvinceIndex = provinceListManagerVertical.curIndex;

//            OnSelectProvince(m_CurProvinceIndex);
//        }
//    }

  
//}

public class Province
{
    public int ProID;
    public string name;
    public int ProSort;
    public string ProRemark;
}
public class City
{
    public int CityID;
    public string name;
    public int ProID;
    public string CitySort;
}